package com.ai.langchain4jchat.config;

import com.ai.langchain4jchat.repository.RedisChatMemoryStore;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

@Configuration
public class CommonConfig {
    @Autowired
    private OpenAiChatModel model;
    /*@Autowired
    private ChatMemoryStore redisChatMemoryStore;*/

   /* @Bean
    public AssistantService assistantService() {
        AssistantService cs = AiServices.builder(AssistantService.class)
                .chatModel(model)//设置对话时使用的模型对象
                .build();
        return cs;
    }*/

    @Bean
    public ChatMemoryStore redisChatMemoryStore(StringRedisTemplate redisTemplate) {
        return new RedisChatMemoryStore(redisTemplate);
    }

    //构建会话记忆对象
    @Bean
    public ChatMemory chatMemory() {
        return MessageWindowChatMemory.builder()
                .maxMessages(20) //最大保存的消息数量
                .build();
    }

    //构建ChatMemoryProvider对象
    @Bean
    public ChatMemoryProvider chatMemoryProvider(ChatMemoryStore redisChatMemoryStore) {
        return new ChatMemoryProvider() {
            @Override
            public ChatMemory get(Object memoryId) {
                return MessageWindowChatMemory.builder()
                        .id(memoryId)
                        .maxMessages(20) //最大保存的消息数量
                        .chatMemoryStore(redisChatMemoryStore) //配置ChatMemoryStore
                        .build();
            }
        };
    }
}
